package com.suredy.app.porject.ctrl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.app.porject.model.BidAgency;
import com.suredy.app.porject.service.BidAgencySrv;
import com.suredy.core.ctrl.BaseCtrl;

/**
 * 招标代理机构控制器
 * 
 * @author VIVID.G
 * @since 2015-10-12
 * @version v0.1
 */
@Controller
@RequestMapping("/config/bid-agency")
public class BidAgencyCtrl extends BaseCtrl {

	@Autowired
	private BidAgencySrv bidAgencySrv;

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(Model model) {
		String view = "app/agency/bid-agency-create";

		return view;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public BidAgency create(BidAgency data) {
		if (data == null)
			return null;

		if (!StringUtils.isBlank(data.getUrl())) {
			if ("http://".equalsIgnoreCase(data.getUrl()))
				data.setUrl(null);
			else
				data.setUrl(data.getUrl().trim());
		}

		BidAgency b = this.bidAgencySrv.saveOrUpdate(data);

		if (b == null || StringUtils.isBlank(b.getId()))
			return null;

		return b;
	}

}
